package main.utils;

import java.awt.Color;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Random;

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class UtilPackage {

	public static Date getCurrentTime(){
		java.util.Date uDate =  new java.util.Date();
		Date sDate = new Date(uDate.getTime());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.format(sDate);
		return sDate;
	}
	
	public static Color getRandomColor(){
		Random rdm = new Random();
		Integer r = rdm.nextInt(255);
		Integer g = rdm.nextInt(255);
		Integer b = rdm.nextInt(255);
		return new Color(r,g,b);
	}
	
	@SuppressWarnings("rawtypes")
	public static Class getSuperClassGenricType(Class clazz, int index) throws IndexOutOfBoundsException {  
		  
        Type genType = clazz.getGenericSuperclass();  
  
        System.out.println(genType);
        if (!(genType instanceof ParameterizedType)) {
        	genType = clazz.getSuperclass().getGenericSuperclass();
//            return Object.class;  
        }  
        System.out.println(genType);
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
  
/*        if (index >= params.length || index < 0) {  
            return Object.class;  
        }  
        if (!(params[index] instanceof Class)) {  
            return Object.class;  
        }  */
        return (Class) params[index];  
    }
	
	public static class MyDateEditor extends PropertiesEditor {    
	    @Override    
	    public void setAsText(String dateStr) throws IllegalArgumentException {  
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        if (dateStr == null || dateStr.equals("")) {
	        	dateStr = sdf.format(new java.util.Date());
//	        	dateStr = UtilPackage.getCurrentTime().toString();    
	        }
	        try {
				setValue(new Date(sdf.parse(dateStr).getTime()));
			} catch (ParseException e) {
				e.printStackTrace();
			}    
	    }    
	    
	    @Override    
	    public String getAsText() {    
	        return getValue().toString();    
	    }    
	}
	
}
